<?php
/**
 * RuiNuoCode低代码后台系统 - 团队十多年低代码经验汇集巨献!
 * =========================================================
 * Copy right 2019-2029 张家界瑞诺科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: https://www.ruinuocrm.com
 * =========================================================
 */
namespace app\setting\service\system;
use app\common\service\Validate as ValidateService;//表单数据验证服务
use app\common\service\Power as PowerService;//表单权限验证服务
use app\common\service\Logic as LogicService;//表单更新服务
use app\common\model\RuinuoSetting as DataMode;//当前模型
use app\setting\service\system\Custom as RuinuoCustom;//自定义方法
use think\facade\Cache;
class Logic extends RuinuoCustom
{

    /*修改->获取表单配置->过滤表单参数->验证表单->验证字段权限->修改完成*/
    public function Edit($params = [])
    {
        if (empty($params['id'])) {
            return ['code' => 203, 'msg' => '参数ID不能为空', 'data' => []];
        }
        $Model = new DataMode();
        $ValidateService = new ValidateService();
        $PowerService = new PowerService();
        $check = $ValidateService->Rule($this->GetVerify(),$params,$params['id']);
        if ($check['code'] == '203') {
            return $check;
        }
        $where="a.id='" . $params['id'] . "'";
        $where.=$PowerService->IndexWhere('setting:system');
        $find = $Model->alias('a')->where($where)->count();
        if ($find == 0) {
            return ['code' => 203, 'msg' => '无此数据或者无权限', 'data' => []];
        }
        $formData =$this->GetFormData($params);
        $ItemData=$PowerService->FormField($formData,'setting:system','edit');
        $ItemData = $this->savebefore($ItemData,$params['id']); //保存前处理
        if (!empty($ItemData['code'])) {
            return ['code' => 203, 'msg' => $ItemData['msg'],'data'=>[]];
        }
        $updata['values']=json_encode($ItemData['a'], JSON_UNESCAPED_UNICODE);
        $Model->where("`id`='" . $params['id'] . "'")->Update($updata);
        $back['id'] = $params['id'];
        $saveafter = $this->saveafter($back['id']);//保存后处理
        if (!empty($saveafter['code'])) {
            return ['code' => 203, 'msg' => $saveafter['msg'],'data'=>[]];
        }
        Cache::set('ruinuo_setting_code_system', '', 600);
        return ['code' => 200, 'msg' => '修改成功', 'data' => $back];
    }


    /*查看->获取表单配置->过滤表单参数->验证字段查看权限->查看完成*/
    public function Detail($params = [])
    {
        $Model = new DataMode();
        $LogicService = new LogicService();
        $PowerService = new PowerService();
        $find = [];
        if (!empty($params['id'])) {
            $where="a.id='" . $params['id'] . "'";
            $where.=$PowerService->IndexWhere('setting:system');
            $find = $Model->alias('a')->where($where)->field("id,values")->findOrEmpty()->toArray();
            if (empty($find)) {
                return ['code' => 203, 'msg' => '无此数据或者无权限', 'data' => []];
            }
            $values=!empty($find['values']) ? json_decode($find['values'],true) : [];
            foreach ($values as $k => $v) {
                $find[$k]=$v;
            }
        }
        $node=!empty($params['node']) ? $params['node'] : 'detail';
        $power=$PowerService->FormField($find,'setting:system','detail',$node);//是否可见权限
        $data['detail']=$power['detail'];
        $data['power']=$power['power'];
        $source='yesno,upload_type';
        $data['source'] = $LogicService->GetSource($source);
        $data = $this->oneafter($data);//查询后处理
        if (!empty($data['code'])) {
            return ['code' => 203, 'msg' => $data['msg'], 'data' => []];
        }
        return ['code' => 200, 'msg' => '请求成功', 'data' => $data];
    }



    /*获取表单参数*/
    public function GetFormData($params = [])
    {
        $LogicService = new LogicService();
        if (isset($params['system_name'])){
            $data['a']['system_name']=strlen($params['system_name']) ? $params['system_name'] : NULL;
        }
        if (isset($params['system_jwt'])){
            $data['a']['system_jwt']=strlen($params['system_jwt']) ? $params['system_jwt'] : NULL;
        }
        if (isset($params['system_logo'])){
            $data['a']['system_logo']=strlen($params['system_logo']) ? $params['system_logo'] : NULL;
        }
        if (isset($params['shop_url'])){
            $data['a']['shop_url']=strlen($params['shop_url']) ? $params['shop_url'] : NULL;
        }
        if (isset($params['baidu_key'])){
            $data['a']['baidu_key']=strlen($params['baidu_key']) ? $params['baidu_key'] : NULL;
        }
        if (isset($params['app_debug'])){
            $data['a']['app_debug']=strlen($params['app_debug']) ? $params['app_debug'] : '2';
        }
        if (isset($params['redis_host'])){
            $data['a']['redis_host']=strlen($params['redis_host']) ? $params['redis_host'] : NULL;
        }
        if (isset($params['redis_port'])){
            $data['a']['redis_port']=strlen($params['redis_port']) ? $params['redis_port'] : NULL;
        }
        if (isset($params['redis_pass'])){
            $data['a']['redis_pass']=strlen($params['redis_pass']) ? $params['redis_pass'] : NULL;
        }
        if (isset($params['file_type'])){
            $data['a']['file_type']=strlen($params['file_type']) ? $params['file_type'] : '1';
        }
        if (isset($params['file_mine'])){
            $data['a']['file_mine']=strlen($params['file_mine']) ? $params['file_mine'] : NULL;
        }
        if (isset($params['file_size'])){
            $data['a']['file_size']=strlen($params['file_size']) ? $params['file_size'] : NULL;
        }
        if (isset($params['file_local'])){
            $data['a']['file_local']=strlen($params['file_local']) ? $params['file_local'] : NULL;
        }
        if (isset($params['aliyun_id'])){
            $data['a']['aliyun_id']=strlen($params['aliyun_id']) ? $params['aliyun_id'] : NULL;
        }
        if (isset($params['aliyun_secret'])){
            $data['a']['aliyun_secret']=strlen($params['aliyun_secret']) ? $params['aliyun_secret'] : NULL;
        }
        if (isset($params['aliyun_bucket'])){
            $data['a']['aliyun_bucket']=strlen($params['aliyun_bucket']) ? $params['aliyun_bucket'] : NULL;
        }
        if (isset($params['aliyun_endpoint'])){
            $data['a']['aliyun_endpoint']=strlen($params['aliyun_endpoint']) ? $params['aliyun_endpoint'] : NULL;
        }
        if (isset($params['aliyun_url'])){
            $data['a']['aliyun_url']=strlen($params['aliyun_url']) ? $params['aliyun_url'] : NULL;
        }
        if (isset($params['qiniu_id'])){
            $data['a']['qiniu_id']=strlen($params['qiniu_id']) ? $params['qiniu_id'] : NULL;
        }
        if (isset($params['qiniu_secret'])){
            $data['a']['qiniu_secret']=strlen($params['qiniu_secret']) ? $params['qiniu_secret'] : NULL;
        }
        if (isset($params['qiniu_bucket'])){
            $data['a']['qiniu_bucket']=strlen($params['qiniu_bucket']) ? $params['qiniu_bucket'] : NULL;
        }
        if (isset($params['qiniu_url'])){
            $data['a']['qiniu_url']=strlen($params['qiniu_url']) ? $params['qiniu_url'] : NULL;
        }
        if (isset($params['qcloud_region'])){
            $data['a']['qcloud_region']=strlen($params['qcloud_region']) ? $params['qcloud_region'] : NULL;
        }
        if (isset($params['qcloud_appid'])){
            $data['a']['qcloud_appid']=strlen($params['qcloud_appid']) ? $params['qcloud_appid'] : NULL;
        }
        if (isset($params['qcloud_id'])){
            $data['a']['qcloud_id']=strlen($params['qcloud_id']) ? $params['qcloud_id'] : NULL;
        }
        if (isset($params['qcloud_secret'])){
            $data['a']['qcloud_secret']=strlen($params['qcloud_secret']) ? $params['qcloud_secret'] : NULL;
        }
        if (isset($params['qcloud_bucket'])){
            $data['a']['qcloud_bucket']=strlen($params['qcloud_bucket']) ? $params['qcloud_bucket'] : NULL;
        }
        if (isset($params['qcloud_url'])){
            $data['a']['qcloud_url']=strlen($params['qcloud_url']) ? $params['qcloud_url'] : NULL;
        }

        return $data;
    }

    /*获取表单验证参数*/
    public function GetVerify()
    {
        $data=[
            ['field'=>'system_name','title'=>'系统名称','rule'=>'required','type'=>'key','tag'=>'input'],
            ['field'=>'system_jwt','title'=>'JWT密钥','rule'=>'required','type'=>'key','tag'=>'input'],
            ['field'=>'system_logo','title'=>'系统logo','rule'=>'required','type'=>'key','tag'=>'upimg'],
            ['field'=>'file_type','title'=>'默认存储方式','rule'=>'required','type'=>'key','tag'=>'radio'],
            ['field'=>'file_local','title'=>'本地存储路径','rule'=>'required','event_type'=>'=','event_val'=>'1','event_field'=>'file_type','type'=>'event'],
            ['field'=>'aliyun_id','title'=>'accessId','rule'=>'required','event_type'=>'=','event_val'=>'2','event_field'=>'file_type','type'=>'event'],
            ['field'=>'aliyun_secret','title'=>'accessSecret','rule'=>'required','event_type'=>'=','event_val'=>'2','event_field'=>'file_type','type'=>'event'],
            ['field'=>'aliyun_bucket','title'=>'bucket','rule'=>'required','event_type'=>'=','event_val'=>'2','event_field'=>'file_type','type'=>'event'],
            ['field'=>'aliyun_endpoint','title'=>'endpoint','rule'=>'required','event_type'=>'=','event_val'=>'2','event_field'=>'file_type','type'=>'event'],
            ['field'=>'aliyun_url','title'=>'url','rule'=>'required','event_type'=>'=','event_val'=>'2','event_field'=>'file_type','type'=>'event'],
            ['field'=>'qiniu_id','title'=>'accessKey','rule'=>'required','event_type'=>'=','event_val'=>'3','event_field'=>'file_type','type'=>'event'],
            ['field'=>'qiniu_secret','title'=>'secretKey','rule'=>'required','event_type'=>'=','event_val'=>'3','event_field'=>'file_type','type'=>'event'],
            ['field'=>'qiniu_bucket','title'=>'bucket','rule'=>'required','event_type'=>'=','event_val'=>'3','event_field'=>'file_type','type'=>'event'],
            ['field'=>'qiniu_url','title'=>'url','rule'=>'required','event_type'=>'=','event_val'=>'3','event_field'=>'file_type','type'=>'event'],
            ['field'=>'qcloud_region','title'=>'region','rule'=>'required','event_type'=>'=','event_val'=>'4','event_field'=>'file_type','type'=>'event'],
            ['field'=>'qcloud_appid','title'=>'appId','rule'=>'required','event_type'=>'=','event_val'=>'4','event_field'=>'file_type','type'=>'event'],
            ['field'=>'qcloud_id','title'=>'secretId','rule'=>'required','event_type'=>'=','event_val'=>'4','event_field'=>'file_type','type'=>'event'],
            ['field'=>'qcloud_secret','title'=>'secretKey','rule'=>'required','event_type'=>'=','event_val'=>'4','event_field'=>'file_type','type'=>'event'],
            ['field'=>'qcloud_bucket','title'=>'bucket','rule'=>'required','event_type'=>'=','event_val'=>'4','event_field'=>'file_type','type'=>'event'],
            ['field'=>'qcloud_url','title'=>'cdn','rule'=>'required','event_type'=>'=','event_val'=>'4','event_field'=>'file_type','type'=>'event'],
            ['field'=>'file_mine','title'=>'允许文件类型','rule'=>'required','type'=>'key','tag'=>'input'],
            ['field'=>'file_size','title'=>'允许文件大小','rule'=>'required','type'=>'key','tag'=>'input'],
            ['field'=>'file_size','title'=>'允许文件大小','rule'=>'isinteger','type'=>'key','tag'=>'input'],
        ];
        return $data;
    }


}